package com.hhh.springai_test.Client;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.hhh.springai_test.Advisor.LoggingAdvisors;
import com.hhh.springai_test.config.ChatConfig;
import com.hhh.springai_test.config.TongYIConfig;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.function.Function;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

@Component
public class TongYiChatClient {

    @Resource
    private TongYIConfig tongYIConfig;

    @Autowired
    @Qualifier("customMemory")  // 明确指定使用你自定义的 ChatMemory 实现
    private ChatMemory chatMemory;


    public ChatClient getTongYiChatClient() {
        DashScopeChatOptions options = DashScopeChatOptions.builder()
                .withModel("qwen-max")
                .withTemperature(0.95F)
                .build();

        // 创建ChatModel对象，传入ZhiPuAiApi和options
        ChatModel chatModel = new DashScopeChatModel(new DashScopeApi(tongYIConfig.getApiKey()), options);
        // 创建ChatClient对象，传入chatModel，设置默认系统消息和默认顾问
        ChatClient chatClient = ChatClient.builder(chatModel).defaultSystem("你现在的身份是一个星轨会议系统的客服助手，请以友好、乐于助人且愉快的方式来回复。我希望你可以以中文的方式给我回答。这里我问你日期的话再回答，否则的话你不要回答")
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory),new LoggingAdvisors())
                .defaultAdvisors(a -> a.param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100))
                .build();
        return chatClient;
    }
}
